# -*- coding: utf-8 -*-
# @Time : 2022-07-05 17:40
# @Author : Z
# @Function :

import json
import time
from captcha import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import wait
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options

# 在打开浏览器之前
option = Options()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 去掉自动化标识
option.add_argument('--disable-blink-features=AutomationControlled')

# 关掉密码弹窗
prefs = {}
prefs['credentials_enable_service'] = False
prefs['profile.password_manager_enabled'] = False
option.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(chrome_options=option)
driver.maximize_window()

# 登陆12306
driver.get('https://kyfw.12306.cn/otn/resources/login.html')
driver.find_element(By.XPATH, '//input[@id="J-userName"]').send_keys('15678300490')
driver.find_element(By.XPATH, '//input[@id="J-password"]').send_keys('7812977zwx')
driver.find_element(By.XPATH, '//*[@id="J-login"]').click()
driver.implicitly_wait(5)
time.sleep(1)

# 处理滑块验证码
track = [380, 400, 500]
for i in track:
    try:
        btn = driver.find_element(By.XPATH, '//*[@id="nc_1__scale_text"]/span')
        ActionChains(driver).drag_and_drop_by_offset(btn, i, 0).perform()
    except:
        time.sleep(0.1)
time.sleep(1)

# 提示
driver.implicitly_wait(3)
driver.find_element(By.XPATH, '//*[text()="确定"]').click()
time.sleep(0.5)

# 悬停到车票
ll = driver.find_element(By.XPATH, '//*[@id="J-chepiao"]/a')
ActionChains(driver).move_to_element(ll).perform()
time.sleep(0.5)
# 点击单程车票
driver.find_element(By.XPATH, '//a[text()="单程"]').click()
# 选择起始点
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').click()
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').send_keys("广州南")
driver.find_element(By.XPATH, '//span[text()="广州南"]').click()
driver.find_element(By.XPATH, '//*[@id="toStationText"]').click()
driver.find_element(By.XPATH, '//*[@id="toStationText"]').send_keys('平南南')
driver.find_element(By.XPATH, '//span[text()="平南南"]').click()
# 选择出发日期
driver.find_element(By.XPATH, '//*[@id="train_date" and @type]').clear()
driver.find_element(By.XPATH, '//*[@id="train_date" and @type]').send_keys('2022-07-15')
driver.find_element(By.XPATH, '//*[text()="查询"]').click()

# trains = ['3806', 'D3730', 'D3722', 'D3846']
trains = ['D3760', 'D3706']
# 获取列车号的元素
trainNum = driver.find_elements(By.XPATH, '//*[@class="number"]')
for text in trainNum:
    # 获取列车号
    num = text.text
    # print(num)
    if num in trains:
        print(num)
        a = '//a[text()="' + str(num) + '"]/../../../../..//td[3]'
        # 获取一等座是否有票
        aa = driver.find_element(By.XPATH, a).get_attribute('aria-label')
        b = '//a[text()="' + str(num) + '"]/../../../../..//td[4]'
        # 获取二等座是否有票
        bb = driver.find_element(By.XPATH, b).get_attribute('aria-label')
        print(aa)
        print(bb)
        # 一等座和二等座都是候补，则无票，继续遍历下个列车
        if aa.endswith('候补') and bb.endswith('候补'):
            print("无票")
        # 一等座是候补，二等座有票，则进行购票，结束循环
        elif aa.endswith('候补') and bb.__contains__('候补') == False:
            c = '//a[text()="' + str(num) + '"]/../../../../..//a[text()="预订"]'
            driver.find_element(By.XPATH, c).click()
            print('购买二等票')
            break
        # 一等座和二等座有票，则进行购票，结束循环
        else:
            d = '//a[text()="' + str(num) + '"]/../../../../..//a[text()="预订"]'
            driver.find_element(By.XPATH, d).click()
            print('购买一等票')
            break
